package com.xywh.wanandroid.compose_ui.score

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Divider
import androidx.compose.material3.FabPosition
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.xywh.composeLibrary.ext.ListFloatingActionButton
import com.xywh.composeLibrary.statex.StateCompose
import com.xywh.composeLibrary.view.XxTopAppBarCenter
import com.xywh.wanandroid.data.model.bean.UserScoreBeanItem
import com.xywh.wanandroid.viewmodel.ScoreComposeViewModel

@Composable
fun ScoreScreen(
    viewModel: ScoreComposeViewModel = hiltViewModel(),
    navigateUp: () -> Unit

) {
    val listState = rememberLazyListState()

    XxTopAppBarCenter(
        title = "积分明细",
        navigationIconOnClick = { navigateUp.invoke() },
        content = { innerPadding ->

            val scoreList = viewModel.scoreList.collectAsLazyPagingItems()

            StateCompose(scoreList) {
                ScoreContent(
                    listState,
                    innerPadding,
                    scoreList
                )
            }

        },
        floatingActionButtonPosition = FabPosition.End,
        floatingActionButton = {
            ListFloatingActionButton(rememberCoroutineScope(), listState)
        })

}


@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ScoreContent(
    listState: LazyListState,
    innerPadding: PaddingValues,
    scoreList: LazyPagingItems<UserScoreBeanItem>

) {

    val refresh by remember { mutableStateOf(false) }
    val pullRefreshState = rememberPullRefreshState(refresh, {
        scoreList.refresh()
    })
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {

        LazyColumn(
            state = listState,
            // consume insets as scaffold doesn't do it by default
            modifier = Modifier
                .consumeWindowInsets(innerPadding),
            contentPadding = innerPadding,
        ) {
            if (scoreList.itemCount > 0) {
                items(
                    count = scoreList.itemCount,
                    key = { index ->
                        val score = scoreList[index]
                        "${score?.id ?: ""}${index}"
                    }) {
                    val itemData = scoreList[it] ?: return@items
                    ScoreItem(
                        itemData,
                    ) { title, link ->

                    }
                }
            }

        }
        PullRefreshIndicator(
            refresh,
            pullRefreshState,
            Modifier.align(Alignment.TopCenter),
            scale = true
        )
    }
}

@Composable
fun ScoreItem(
    userScoreBeanItem: UserScoreBeanItem,
    itemOnClick: (title: String, link: String) -> Unit
) {
    ListItem(
        modifier = Modifier.clickable {

        },
        headlineContent = {
            Text(
                userScoreBeanItem.reason,
                fontSize = 14.sp,
                color = Color.Black,
                modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth(),
            )
        },
//        overlineContent = {
//
//        },
        supportingContent = {


            Row(
                Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {

                Text(
                    userScoreBeanItem.desc,
                    fontSize = 12.sp,
                    color = Color(0xFF787878)
                )

            }
        },
        //                        leadingContent = {
        //
        //                        },
        trailingContent = {
            Text(
                "+" + userScoreBeanItem.coinCount.toString(),
                fontSize = 12.sp,
                color = Color(0xFF00BCD48)
            )
        }
    )
    Divider()
}
